import React from 'react'
import { Input } from 'antd';
import { Select } from 'antd';
import { Form } from 'antd';
import { Divider } from 'antd';
import { Image } from 'antd';
import { Radio } from 'antd';
import { Upload, message } from 'antd';
import { LoadingOutlined, PlusOutlined } from '@ant-design/icons';

const { Option } = Select;
const { TextArea } = Input;
function getBase64(img, callback) {
    const reader = new FileReader();
    reader.addEventListener('load', () => callback(reader.result));
    reader.readAsDataURL(img);
}

function beforeUpload(file) {
    const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
    if (!isJpgOrPng) {
        message.error('You can only upload JPG/PNG file!');
    }
    const isLt2M = file.size / 1024 / 1024 < 2;
    if (!isLt2M) {
        message.error('Image must smaller than 2MB!');
    }
    return isJpgOrPng && isLt2M;
}

class AddCategory extends React.Component {
    state = {
        loading: false,
    };

    handleChange = info => {
        if (info.file.status === 'uploading') {
            this.setState({ loading: true });
            return;
        }
        if (info.file.status === 'done') {
            // Get this url from response in real world.
            getBase64(info.file.originFileObj, imageUrl =>
                this.setState({
                    imageUrl,
                    loading: false,
                }),
            );
        }
    };

    render() {
        const { loading, imageUrl } = this.state;
        const uploadButton = (
            <div>
                {loading ? <LoadingOutlined /> : <PlusOutlined />}
                <div style={{ marginTop: 8 }}>Upload</div>
            </div>
        );
        return (
            <div>
                <Form.Item
                    name={['user', '分类名称']}
                    label="分类名称"
                    rules={[
                        {
                            type: '分类名称',
                        },
                    ]}
                >
                    <Input style={{ width:350 }} />
                </Form.Item>
                <Divider />
                <Form.Item
                    name={['user', '分类logo']}
                    label="分类logo"
                    rules={[
                        {
                            type: '分类logo',
                        },
                    ]}
                >
                    <Upload
                        name="avatar"
                        listType="picture-card"
                        className="avatar-uploader"
                        showUploadList={false}
                        action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
                        beforeUpload={beforeUpload}
                        onChange={this.handleChange}
                    >
                        {imageUrl ? <img src={imageUrl} alt="avatar" style={{ width: '100%' }} /> : uploadButton}
                    </Upload>
                </Form.Item>
                <Divider />
                <Form.Item label="上级分类">
                    <Input.Group compact>
                        <Form.Item
                            name={['class', 'classify']}
                            noStyle
                            rules={[{ required: true, message: 'classify is required' }]}
                        >
                            <Select style={{ width:240 }} placeholder={"请选择"}>
                                <Option value="请选择">请选择</Option>
                                <Option value="1">生鲜水果</Option>
                                <Option value="2">食品粮油</Option>
                                <Option value="3">其他分类</Option>
                            </Select>
                        </Form.Item>
                    </Input.Group>
                </Form.Item>
                <Divider />
                <Form.Item
                    name={['user', '数量单位']}
                    label="数量单位"
                    rules={[
                        {
                            type: '数量单位',
                        },
                    ]}
                >
                    <Input style={{ width:350 }} />
                </Form.Item>
                <Divider />
                <Form.Item
                    name={['user', '分类排序']}
                    label="分类排序"
                    rules={[
                        {
                            type: '分类排序',
                        },
                    ]}
                >
                    <Input style={{ width:350 }} />
                </Form.Item>
                <Divider />
                <h4>是否显示:</h4>
                <Radio.Group>
                    <Radio value={1}>是</Radio>
                    <Radio value={2}>否</Radio>
                </Radio.Group>
                <Divider />
                <Form.Item
                    name={['user', '关键字']}
                    label="关键字"
                    rules={[
                        {
                            type: '关键字',
                        },
                    ]}
                >
                    <Input style={{ width:350 }} />
                </Form.Item>
                <Divider />
                <Form.Item name={['user', 'describe']} label="分类描述">
                    <TextArea rows={4} style={{ width:600 }} />
                </Form.Item>
            </div>
        )
    }
}

export {AddCategory as default}